<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 面包屑导航 -->
        <el-breadcrumb
          separator-class="el-icon-arrow-right"
          v-if="$route.query.id"
          style="margin-bottom: 20px;
            border-bottom: 1px solid #ebeef5;
            height: 31px;
            line-height: 31px;
        "
        >
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 目录管理 -->
        <el-row>
          <el-col :span="18">
            <el-form
              :inline="true"
              :model="dirForm"
              class="demo-form-inline"
            >
              <el-form-item label="目录名称">
                <el-input v-model="dirForm.directoryName"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="dirForm.state">
                  <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
                  <!-- <el-option label="禁用" value="jy"></el-option> -->
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="deleSubmit">清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-button type="text"
            @click="$router.push('/subjects/list')"
            v-if="$route.query.id"><i class="el-icon-back"></i>返回学科</el-button>
            <el-button type="success" @click="showDialog"
              ><i class="el-icon-edit"></i>新增目录</el-button
            >
          </el-col>
        </el-row>
        <!-- 底部数据提示 -->
        <el-alert :title="title" type="info" show-icon :closable="false">
        </el-alert>
        <!-- 表格数据 -->
        <el-table :data="list" style="width: 100%" :header-cell-style="{background:'#fafafa',color:'#606266'}">
          <el-table-column label="序号" type="index" width="100">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="180">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template #default="{ row }">
              {{ dateFormat(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
          <el-table-column prop="state" label="状态">
            <template #default="{ row }">
              {{ directorysForm[row.state] }}
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作">
            <template #default="{row}">
              <el-button
                @click="handleClick(row)"
                type="text"
                size="small"
                >{{row.state?'禁用':'启用'}}</el-button
              >
              <el-button type="text" size="small" :disabled="row.state === 1" @click="dirText(row.id)">修改</el-button>
              <el-button type="text" size="small" :disabled="row.state === 1" @click="deleteDir(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <div class="block">
          <!-- <span class="demonstration">完整功能</span> -->
          <el-pagination
            style="margin-top: 20px; text-align: right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.page"
            background
            :page-sizes="[5, 10, 20, 50]"
            :page-size="params.pagesize"
            layout="prev, pager, next,sizes,jumper"
            :total="counts"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
    <directorys-add :dialogVisible="dialogVisible" ref="dirRefForm" @input="closeDialog" @listForm="listDialog" :dirID="dirID" @search="search" @Page="Page"></directorys-add>
  </div>
</template>

<script>
import directorysAdd from '../components/directorys-add.vue'
import dayjs from 'dayjs'
import { list, changeState, remove } from '@/api/hmmm//directorys.js'
import { status } from '@/api/hmmm/constants.js'

export default {
  components: {
    directorysAdd
  },
  created () {
    this.listForm()
  },
  data () {
    return {
      status: status,
      dirForm: {
        directoryName: '',
        state: ''
      },
      dialogVisible: false,
      title: '',
      directorysForm: {
        1: '已启用',
        0: '已禁用'
      },
      params: {
        page: 1,
        pagesize: 10
      },
      dirID: 0,
      disabled: false,
      counts: 0,
      list: []
    }
  },
  methods: {
    async listForm () {
      let res = {}
      if (this.$route.query.id) {
        res = await list({
          ...this.params,
          subjectID: this.$route.query.id
        })
      } else {
        res = await list(this.params)
      }
      console.log(res)
      this.list = res.data.items
      this.counts = res.data.counts
      this.title = `数据一共${this.counts}条`
    },
    closeDialog () {
      this.dialogVisible = false
      this.dirID = 0
    },
    showDialog () {
      this.dialogVisible = true
    },
    listDialog () {
      this.listForm()
    },
    deleSubmit () {
      this.$route.query.id = ''
      this.dirForm.directoryName = ''
      this.dirForm.state = ''
      this.params.page = 1
      this.params.pagesize = 10
      this.listForm()
    },
    async onSubmit () {
      // console.log('submit!')
      this.params.page = 1
      this.search()
    },
    async search () {
      if (this.dirForm.directoryName === '' && this.dirForm.state === '') {
        this.listForm()
      } else {
        let res = {}
        if (this.dirForm.state === '') {
          res = await list({ directoryName: this.dirForm.directoryName, ...this.params })
        } else {
          res = await list({ ...this.dirForm, ...this.params })
        }

        // console.log(res.data.items)
        this.list = res.data.items
        this.counts = res.data.counts
        this.title = `数据一共${this.counts}条`
      // this.listForm()
      }
    },
    dateFormat (str) {
      return dayjs(str).format('YYYY-MM-DD HH:mm:ss')
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.params.pagesize = val
      this.search()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.params.page = val
      this.search()
    },
    async handleClick (row) {
      // const res = await changeState({ id: data.id, state: data.state })
      // console.log('你好啊啊啊啊啊啊啊啊', res)
      if (row.state === 1) {
        row.state = 0
        this.disabled = false
      } else {
        row.state = 1
        this.disabled = true
      }
      this.$message.success('操作成功')
      // 发起请求，更新后台数据
      await changeState(row)
    },
    deleteDir (id) {
      // 询问用户
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起删除请求
        await remove({ id: id })
        // 提示用户
        this.$message.success('删除成功')
        // 刷新表格数据
        // this.listForm()
        this.search()
      })
    },
    // 修改数据
    dirText (id) {
      this.dirID = id
      this.dialogVisible = true
      this.$refs.dirRefForm.dirText(id)
    },
    // 修改页码
    Page () {
      this.params.page = 1
    }
  }
}
</script>

<style scoped></style>
